package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.CandidateDTO;
import com.lastproject.ejb.entities.Candidate;

@Stateless
@LocalBean
public class CandidateConversion {
	
	@PersistenceContext
	EntityManager em;

	public CandidateDTO fromEntity(Candidate candidate){
		
		CandidateDTO candidateDTO = new CandidateDTO();
		candidateDTO.setId(candidate.getId());
		candidateDTO.setFirstname(candidate.getFirstname());
		candidateDTO.setLastname(candidate.getLastname());
		candidateDTO.setBirthdate(candidate.getBirthdate());
		candidateDTO.setApplyingFor(candidate.getApplyingFor());
		candidateDTO.setDomain(candidate.getDomain());
		candidateDTO.setStatut(candidate.getStatut());
		candidateDTO.setPhoneNr(candidate.getPhoneNr());
		candidateDTO.setEmail(candidate.getEmail());
		candidateDTO.setCv(candidate.getCv());
		candidateDTO.setPhoto(candidate.getPhoto());
		
		return candidateDTO;

	}
	
	public Candidate fromDTO(CandidateDTO candidateDTO){

			Candidate candidate = em.find(Candidate.class, candidateDTO.getId());

			if (candidate == null) {
				candidate = new Candidate();
			}

			candidate.setFirstname(candidateDTO.getFirstname());
			candidate.setLastname(candidateDTO.getLastname());
			candidate.setBirthdate(candidateDTO.getBirthdate());
			candidate.setApplyingFor(candidateDTO.getApplyingFor());
			candidate.setDomain(candidateDTO.getDomain());
			candidate.setStatut(candidateDTO.getStatut());
			candidate.setPhoneNr(candidateDTO.getPhoneNr());
			candidate.setEmail(candidateDTO.getEmail());
			candidate.setCv(candidateDTO.getCv());
			candidate.setPhoto(candidateDTO.getPhoto());
			
			return candidate;
		
	}
	
	
}
